package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.exception.BusinessException;
import com.demo.pojo.dto.TeacherListDTO;
import com.demo.pojo.entity.Teacher;
import com.demo.service.TeacherService;
import com.demo.mapper.TeacherMapper;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
* @author lenovo
* @description 针对表【teacher】的数据库操作Service实现
* @createDate 2025-09-11 17:15:14
*/
@Service
@Slf4j
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService{

    private final TeacherMapper teacherMapper;

    public TeacherServiceImpl(TeacherMapper teacherMapper) {
        this.teacherMapper = teacherMapper;
    }


    @Override
    public Teacher selectById(Integer id) {
        //根据教师id查询教师信息
        Teacher teacher = teacherMapper.selectById(id);
        //如果没查到抛出异常
        if (teacher == null){
            throw new BusinessException("没有此教师信息");
        }
        log.info("查询教师信息成功:{}", teacher);
        return teacher;
    }

    @Override
    public void saveTeacher(Teacher teacher) {
        //新增前判断工号有无重复
        if (teacherMapper.selectByNO(teacher.getTeNo()) != null){
            throw new BusinessException("工号重复");
        }
        //执行新增操作
        teacherMapper.insert(teacher);
        log.info("新增教师信息成功:{}", teacher);
    }


    @Override
    public void updateTeacher(Teacher teacher) {
        //修改前判断是否存在该教师
        Teacher oldTeacher = teacherMapper.selectById(teacher.getTeacherId());
        if (oldTeacher == null){
            throw new BusinessException("没有此教师信息");
        }
        //修改前判断工号有无变动
        //如果查询出的工号和修改前的工号一致，则不进行修改
        if ( !oldTeacher.getTeNo().equals(teacher.getTeNo())){
           //工号不一致，说明工号被修改，查询有无重复
            if (teacherMapper.selectByNO(teacher.getTeNo()) != null){
                throw new BusinessException("工号重复");
            }
        }
        //设置修改时间
        teacher.setTeUpdateDate(new Date());
        //执行修改操作
        teacherMapper.updateById(teacher);
        log.info("修改教师信息成功:{}", teacher);
    }

    @Override
    public Map<String, Object> listPage(TeacherListDTO dto) {
        //1. 创建分页对象
        Page<Teacher> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        // 2. 构建查询条件
        LambdaQueryWrapper<Teacher> lambdaQueryWrapper = new LambdaQueryWrapper<Teacher>()
                .eq(dto.getTeNo() != -1,Teacher::getTeNo,dto.getTeNo())
                .like(StringUtils.isNotBlank(dto.getTeName()),Teacher::getTeName,dto.getTeName())
                .eq(dto.getTeSex() != -1,Teacher::getTeSex,dto.getTeSex())
                .like(StringUtils.isNotBlank(dto.getTePhone()),Teacher::getTePhone,dto.getTePhone());

        // 3. 执行分页查询
        Page<Teacher> pageResult = teacherMapper.selectPage(page, lambdaQueryWrapper);

        log.info("分页查询结果：{}", pageResult);
        // 4. 返回结果
        return Map.of(
                "records", pageResult.getRecords(),
                "total", pageResult.getTotal(),
                "current", pageResult.getCurrent()
        );
    }

    @Override
    public Teacher login(Long teNo, String idTail6) {
        Teacher teacher = teacherMapper.selectByNO(teNo);
        if (teacher == null) {
            throw new BusinessException("工号不存在");
        }
        String idCard = teacher.getTeIdCard();
        if (idCard == null || idCard.length() < 6) {
            throw new BusinessException("系统未配置身份证号");
        }
        String tail6 = idCard.substring(idCard.length() - 6);
        if (!tail6.equalsIgnoreCase(idTail6)) {
            throw new BusinessException("身份证后六位不正确");
        }
        return teacher;
    }


}




